package com.awspaas.user.apps.processforsap.event;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListener;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.processforsap.util.Logs;
import com.awspaas.user.apps.processforsap.util.ParentInterface;

import java.lang.reflect.Constructor;
import java.util.UUID;

public class UniversalBPMToSAPProcessEndMM004 extends InterruptListener {


    @Override
    public boolean execute(ProcessExecutionContext pre) {
        //按钮判断
        ProcessInstance processInstance = pre.getProcessInstance();
        String bindId = processInstance.getId();
        String interfaceId = "BPM2SAP_MM005";
        if (interfaceId.contains("@")) {
            interfaceId = SDK.getRuleAPI().executeAtScript(interfaceId, pre.getUserContext(), bindId, null, null);
        }
        String interfaceName = "";
        String logId = "";
        String resultSendData = "";
        String returnData = "";
        ParentInterface parentInterface = null;
        String url = "";
        //处理多个请求的情况
        String inters[] = interfaceId.split(",");
        for (String inter : inters) {
            Object result = SDK.getProcessAPI().getVariable(processInstance.getId(), inter);
            if (result != null) {
                Boolean resultJG = Boolean.valueOf(result.toString());
                if (resultJG) {
                    continue;
                }
            }
            try {
                //查询接口维护配置表
                BO bo = SDK.getBOAPI().query("BO_EU_SERVICE_INTERFACE_CONFIG").addQuery("INTERFACE_NO=", inter).detail();
                if (bo == null) {
                    throw new BPMNError("0331", "未找到配置表数据，请核查BPM配置！");
                } else {
                    /**
                     * 业务代码
                     */
                    String code = bo.getString("CODE");
                    /**
                     * 接口名称
                     */
                    logId = UUID.randomUUID().toString();
                    interfaceName = bo.getString("INTERFACE_NAME");
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("BINDID", bindId);
                    jsonObject.put("LOGID", logId);
                    jsonObject.put("SPDM","拒签");
                    JSONObject messageHeader = new JSONObject();
                    messageHeader.put("Interface_ID", inter);
                    messageHeader.put("UUID", bindId);
                    messageHeader.put("MessageId", "");
                    messageHeader.put("DisId", "");
                    jsonObject.put("MessageHeader", messageHeader);
                    Class<?> clzz = Class.forName(code);
                    Constructor<?> con = clzz.getConstructor(JSONObject.class);
                    parentInterface = (ParentInterface) con.newInstance(jsonObject);
                    parentInterface.dueInterFaceData();
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new BPMNError("0331", e.getMessage());
            } finally {
                resultSendData = parentInterface.resultSendData;
                returnData = parentInterface.returnData;
                parentInterface.resultType = false;
                try {
                    JSONObject returnJSON = JSONObject.parseObject(returnData);
                    JSONObject responses = returnJSON.getJSONObject("Responses");
                    parentInterface.resultType = "S".equals(responses.getString("Type")) ? true : false;
//                    returnData = responses.getString("MESSAGE");
                } catch (Exception e) {
                    e.printStackTrace();
//                    returnData = e.getMessage();
                    parentInterface.resultType = false;
                }
                Logs.addLog(interfaceName, interfaceId, "send", "BPM", "SAP", resultSendData, returnData, parentInterface.resultType, "", "1", bindId, "", logId);
                if (parentInterface.resultType) {
                    SDK.getProcessAPI().setVariable(processInstance, inter, true);
                } else {
                    throw new BPMNError("0331", returnData);
                }
            }
        }
        return true;
    }
}